package cat.music.ut.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.Toast
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.viewbinding.ViewBinding
import cat.music.ut.CatApp
import cat.music.ut.R
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment


abstract class BaseBottomFragment<VBinding : ViewBinding> : BottomSheetDialogFragment() {
    val sp by lazy { CatApp.getInstance().getSp() }
    protected lateinit var binding: VBinding
    protected lateinit var vb: VBinding
    protected abstract fun getViewBinding(): VBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
        init()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        dialog!!.setOnShowListener { dialog ->
//            val d = dialog as BottomSheetDialog
//            val bottomSheet =
//                d.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
//            val coordinatorLayout = bottomSheet!!.parent as CoordinatorLayout
//            val bottomSheetBehavior: BottomSheetBehavior<*> =
//                BottomSheetBehavior.from(bottomSheet!!)
//            bottomSheetBehavior.peekHeight = 360
//            bottomSheet.layoutParams.height = 360
//            bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
//            coordinatorLayout.parent.requestLayout()
//        }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
        observe()
    }

    abstract fun initView()

    open fun observe() {}

    /**
     * 初始化数据
     */
    open fun initData() {}
    open fun updateData() {}

    private fun init() {
        binding = getViewBinding()
        vb = binding

    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    fun toast(msg: String) {
        requireActivity().runOnUiThread {
            Toast.makeText(requireActivity(), msg, Toast.LENGTH_SHORT).show()
        }
    }

    fun toast(res: Int) {
        toast(getString(res))
    }

    open fun show(activity: BaseActivity<*>) {
        super.show(activity.supportFragmentManager, activity::class.java.simpleName)
    }

    private val activity by lazy { requireActivity() as BaseActivity<*> }
    fun showLoading() {
        activity.showLoading()
    }

    fun showLoading(msg: String) {
        activity.showLoading(msg)
    }

    fun hideLoading() {
        activity.hideLoading()
    }

    override fun onDestroy() {
        super.onDestroy()
        hideLoading()
    }
}